import request from '../../common/request';
import {xzl,wlq_order} from '../../common/config';

Page({
  data:{
    order_note:'',
    goodsId:'',
    goods_detail:{}
  },

  // 输入框数据
  handleInput(e){
    this.setData({
      order_note:e.detail.value
    })
  },

  // 获取商品信息
  async getGoodsDetail(goodsId){
    const URL = `${xzl}/getGoodsDetail/${goodsId}`;
    const res = await request(URL);
    this.setData({
      goods_detail:res.data
    })
  },

  // 提交订单
 async handleSubmit(){
    const token = wx.getStorageSync('cookie');
    const {goodsId,order_note} = this.data;
    const URL = `${wlq_order}/order?access_token=${token}`;

    const res = await request(URL,'POST',JSON.stringify({
      goodsId:goodsId,
      remarks:order_note
    }));

    if(res.code!==200){
      console.log('下单失败',res)
      wx.showModal({
        title: '提示',
        content: res.message,
        showCancel: false,
        confirmText: '知道了',
        confirmColor: '#3CC51F',
      });
      return;
    }

    wx.showModal({
      title: '下单成功了',
      content: '请到我的订单查看订单',
      showCancel: false,
      confirmText: '知道了',
      confirmColor: '#3CC51F',
      success: (result) => {
        if (result.confirm) {
          // 跳转到我的订单
          wx.requestSubscribeMessage({
            tmplIds: ['b2zFWZbu075wBg0TfMcpMO8oeKQFq1YsUkpwxrtly94'],
            success (res) { 
              console.log(res)
            }
          })
        }
      }
    });
  },

  onLoad(options){
    this.setData({
      goodsId:options.goodsId
    })
    this.getGoodsDetail(options.goodsId)
  }

})